package cn.onlineclass.common;

import cn.onlineclass.entity.OBook;
import cn.onlineclass.service.BookService;
import cn.onlineclass.util.StringUtil;
import cn.onlineclass.vo.AjaxPage;
import cn.onlineclass.vo.AjaxResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

public class BookConntroller {

    public AjaxResult add(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        String name = request.getParameter("name");
        String imgsrc = request.getParameter("imgsrc");
        long unitid = StringUtil.toLong(request.getParameter("unitid"),0);
        long viewers =StringUtil.toLong(request.getParameter("viewers"),0);
        System.out.println(unitid);
//        long unitid = Integer.parseInt(request.getParameter("unitid"),0);
//        long viewers =Integer.parseInt(request.getParameter("viewers"),0);
        String descrption =request.getParameter("descrption");
        boolean inuse = StringUtil.isBlank(request.getParameter("inuse"));
//        String createtime = request.getParameter("createtime");
//        Date cd = StringUtil.parse(createtime);
//        String modifytime = request.getParameter("modifytime");
//        Date md = StringUtil.parse(modifytime);
        OBook book = new OBook(name,unitid,descrption,inuse,null,null,viewers,imgsrc);
        AjaxResult num = BookService.INST.insertOBook(book);
        String str = JSON.toJSONString(num);
        response.getWriter().print(str);
        return num;
    }

    public AjaxResult del(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        AjaxResult num = BookService.INST.del(id);
        String str = JSON.toJSONString(num);
        response.getWriter().print(str);
        return null;
    }
    public AjaxResult update(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String imgsrc = request.getParameter("imgsrc");
        long unitid = StringUtil.toLong(request.getParameter("unitid"),0);
        long viewers =StringUtil.toLong(request.getParameter("viewers"),0);
        String descrption =request.getParameter("descrption");
        boolean inuse = StringUtil.isBlank(request.getParameter("inuse"));
        
//        String createtime = request.getParameter("createtime");
//        Date cd = StringUtil.parse(createtime);
//        String modifytime = request.getParameter("modifytime");
//        Date md = StringUtil.parse(modifytime);
        OBook book = new OBook(id,name,unitid,descrption,inuse,null,null,viewers,imgsrc);
        AjaxResult num = BookService.INST.update(book);
        String str = JSON.toJSONString(num);
        response.getWriter().print(str);
        return num;
    }
    public void search(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
//        int pageNo= StringUtil.toInt(request.getParameter("pageNo"),1);
//        AjaxPage ajaxPage = BookService.INST.search(pageNo,10);
//        String str = JSON.toJSONString(ajaxPage);
//        response.getWriter().print(str);
        int pageNo = StringUtil.toInt(request.getParameter("pageNo"),1);
        int pageSize = StringUtil.toInt(request.getParameter("pagaSize"),10);
        AjaxPage search = BookService.INST.searchAll(pageNo,pageSize);
        String str = JSON.toJSONStringWithDateFormat(search,"yyyy-MM-dd", SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNonStringValueAsString);
        response.getWriter().print(str);
    }
}
